import {Component} from "@angular/core";

import {AlertController, IonicPage, NavController, NavParams, Platform} from 'ionic-angular';
import {Browser} from '../../providers/providers';
import {User} from "../../providers/user/user";
import {Constant} from "../../providers/constant/constant";

/**
 * Generated class for the ProfilePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-profile',
  templateUrl: 'profile.html',
})
export class ProfilePage {

  private profile: any;
  private version: string;

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private user: User,
              private constant: Constant,
              private alertCtrl: AlertController,
              public platform: Platform,
              public browser: Browser) {
    this.profile = this.user._user;
    this.version = this.constant.VERSION;
  }

  ionViewDidLoad() {
    console.log('ionViewDidLoad ProfilePage');
  }

  openProjects(URL) {
    this.browser.launch(URL);
  }

  openSystemBrower(URL) {
    this.browser.launchWithTarget(URL, "_system");
  }

  singOut() {
    this.user.logout();
    this.platform.exitApp();
    this.navCtrl.push('LoginPage', {}, {
      animate: true,
      direction: 'forward'
    });
  }

  changePwd() {
    let alert = this.alertCtrl.create({
      title: '重置密码',
      inputs: [
        {
          name: 'password',
          placeholder: '新密码'
        },
      ],
      buttons: [
        {
          text: '取消',
          handler: () => {
            console.log('Cancel clicked');
          }
        },
        {
          text: '保存',
          handler: (e) => {
            this.user.changePassword(e);
          }
        }
      ]
    });

    alert.present();
  }

  customerManage() {
    this.navCtrl.push('CustomerManagePage', {}, {
      animate: true,
      direction: 'forward'
    });
  }

  agencyManage() {
    this.navCtrl.push('AgencyManagePage', {}, {
      animate: true,
      direction: 'forward'
    });
  }
}
